import WXAD from "../../../StartAb/WXAd";
import EventCenter from "../../TFrame/script/helper/EventCenter";
import EventNames from "../EventNames";
import NativeCaller from "../NativeCaller";
import { ActionType } from "../net/Enum";
import ServiceLogic from "../net/ServerLogic";
import ToastHelper from "../ui/ToastHelper";
import CoinSystem from "./CoinSystem";

interface ProductInfo {
    PriceInFormat: string
}

export default class IAPSystem {

    static AD = "com.sevendaypuzzle.finddifference2.removead";
    static coin200 = "com.sevendaypuzzle.finddifference2.200coins";
    static coin500 = "com.sevendaypuzzle.finddifference2.500coins";
    static coin3000 = "com.sevendaypuzzle.finddifference2.3000coins"

    static key_iapAdRemoved: string = "key_iapAdRemoved"
    static _isAdRemoved = null;
    static productInfoMap = {} as { string: ProductInfo }

    static IAPEnabled = false;

    static load() {
        IAPSystem._isAdRemoved = cc.sys.localStorage.getItem(IAPSystem.key_iapAdRemoved)

        window['syncProductInfoToJs'] = IAPSystem.syncProductInfoNativeToJs;
        window['iapBuyResult'] = IAPSystem.iapBuyResultNativeToJs
        window['requestBuyFailed'] = IAPSystem.requestBuyFailedNativeToJs;
    }


    //服务端传过来的商品信息
    static syncProductInfoNativeToJs(str: string) {
        // let products = str.split(";");
        // for (let i = 0; i < products.length; ++i) {
        //     let productStr = products[i];
        //     if (productStr.length > 0) {
        //         let infos = productStr.split(",");
        //         if (infos.length >= 2) {
        //             let iapId = infos[0];
        //             let priceStr = infos[1];
        //             IAPSystem.productInfoMap[iapId] = { "PriceInFormat": priceStr }
        //         }
        //     }
        // }
        IAPSystem.IAPEnabled = str.length > 4
        ServiceLogic.action(ActionType.ActionIAPInfoSubmit, { 'beiz': str })
    }

    static iapBuyResultNativeToJs(iapId, ret) {
        console.log("IAD iapBuyResult iapid:%s, ret:%s", iapId, ret)
        if (ret == 1) {
            if (iapId == IAPSystem.AD) {
                IAPSystem._isAdRemoved = true;
                WXAD.instance.hideBanner()
                cc.sys.localStorage.setItem(IAPSystem.key_iapAdRemoved, "1")
            }
            EventCenter.emit(EventNames.IAPSuccess, iapId)
            ServiceLogic.action(ActionType.ActionIAPBuySuccess, { "coinhave": CoinSystem.coinInMomory, "beiz": iapId });
        }
    }

    static requestBuyFailedNativeToJs(code: number, msg: string) {
        console.log("IAD requestBuyFailedNativeToJs code:%d, msg:%s", code, msg)
        if (code == 0) {

        } else {
            ToastHelper.show("Sorry, Purchase Failed");
            ServiceLogic.action(ActionType.ActionIAPBuyFailed, { 'beiz': msg })
        }
    }


    static isAdRemoved() {
        return IAPSystem._isAdRemoved != null;
    }


    static getIAPCoinNum(iapId: string) {
        switch (iapId) {
            case IAPSystem.coin200:
                return 200;
            case IAPSystem.coin500:
                return 500;
            case IAPSystem.coin3000:
                return 3000;
        }
        return 0;
    }
}
